package cn.tedu.v7;

import java.io.*;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Client {
    private static final String HOST="127.0.0.1";
    private static final Integer PORT=3333;
    private Socket socket;
    private Scanner scanner;
    private String username;
    private String password;
    private String serverMessage;
    private BufferedReader br;
    private PrintWriter pw;
    private String ip;
    public Client(){

        try {
            socket=new  Socket(HOST,PORT);
            ip=socket.getInetAddress().getHostAddress();
            br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF8"));
            pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8")), true);
        } catch (SocketException e) {
            System.out.println("连接被重置或者断开");
        } catch (UnknownHostException e) {
            System.out.println("IP地址有问题，请询问服务器端");
        }catch (IOException e) {
            System.out.println("客户端主动退出了");
        }

    }
    private void chat() {
        System.out.println("——————欢迎进入聊天室—————");
        new Thread(() -> {
            try {
                String message;
                while ((message = br.readLine()) != null) {
                    System.out.println(message/*br.readLine()*/);
                }
            } catch (IOException e) {
                System.out.println("服务器端断开了");
            }
        }).start();
        System.out.println(username+ "客户端：请输入文字：");
        Scanner input = new Scanner(System.in);
        while (true) {
            String say = input.nextLine();
            pw.println("客户端说" + say);

            if (say.equalsIgnoreCase("exit")) {
                System.out.println("退出啦");
                break;
            }
        }
    }
    public void start() {

        try {
            Scanner input = new Scanner(System.in);
            serverMessage = br.readLine();
            System.out.println(serverMessage);
            username = input.nextLine();
            pw.println(username);
            serverMessage=br.readLine();
            System.out.println(serverMessage);
            password=input.nextLine();
            pw.println(password);
            serverMessage=br.readLine();
            System.out.println(serverMessage);
            if (serverMessage.startsWith("登陆成功，欢迎")) {
                chat();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (br != null)
                try {
                    br.close();
                } catch (IOException e) {
                    System.out.println("客户端离开");
                }
            if (pw != null)
                pw.close();
            try {
                socket.close();
            } catch (IOException e) {
                System.out.println("客户端离开");
            }
            if (socket != null && !socket.isClosed()) {
                System.out.println(ip + "客户端关闭成功");
                try {
                    socket.close();
                } catch (IOException e) {
                    System.out.println(ip + "客户端关闭失败");
                }
            }
        }
    }


    public static void main(String[] args) {
        new Client().start();

    }
}